package com.nlecloud.bc.controller.contract;

import com.nlecloud.bc.constant.CustomerConstant;
import com.nlecloud.bc.controller.contract.dto.CopyrightDTO;
import com.nlecloud.bc.interceptor.AuthTokenStorage;
import com.nlecloud.bc.model.PageResult;
import com.nlecloud.bc.model.Result;
import com.nlecloud.bc.model.User;
import com.nlecloud.bc.service.contract.ICopyrightContractService;
import org.fisco.bcos.sdk.transaction.model.CommonConstant;
import org.fisco.bcos.sdk.transaction.model.exception.ContractException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * @author warrior
 * @description 版权合约API
 */
@RestController
public class CopyrightContractController {

    @Autowired
    private ICopyrightContractService contractCopyrightService;

    /**
     * 新增版权作品信息
     * @param copyrightDTO 版权作品信息传输封装对象
     * @return
     */
    @PostMapping("contract/copyrights")
    public Result addCopyright(@RequestBody CopyrightDTO copyrightDTO) throws IOException {
        boolean success = contractCopyrightService.addCopyright(copyrightDTO.getTitle(), copyrightDTO.getPath(),copyrightDTO.getAuthor());
        if (!success) {
            return Result.failed("新增版权作品信息失败");
        }
        return Result.succeed("新增版权作品信息成功");
    }

    /**
     * 查询拥有者的版权作品信息
     *
     * @param index 从第几条数据查询
     * @param size  查询条数
     * @return
     */
    @GetMapping("contract/owner/copyrights")
    public Result<PageResult<CopyrightDTO>> listCopyrightByOwnerId(@RequestParam("index") int index, @RequestParam("size") int size, HttpServletRequest req) throws ContractException {
        User user = AuthTokenStorage.getUser();
        String ownerId = user.getId();
        PageResult<CopyrightDTO> copyrightDTOS = contractCopyrightService.listCopyrightByOwnerId(ownerId, index, size, req);
        return Result.succeed(copyrightDTOS);
    }

    /**
     * 通过转让状态查询版权作品信息
     *
     * @param status 可转让状态：1 不可转让，2 可转让
     * @param index  从第几条数据查询
     * @param size   查询条数
     * @return
     */
    @GetMapping("contract/copyrights")
    public Result<PageResult<CopyrightDTO>> listCopyrightByStatus(@RequestParam("status") int status, @RequestParam("index") int index, @RequestParam("size") int size, HttpServletRequest req) throws ContractException {
        PageResult<CopyrightDTO> copyrightDTOS = contractCopyrightService.listCopyrightByStatus(status, index, size, req);
        return Result.succeed(copyrightDTOS);
    }

    /**
     * 设置版权转让信息
     * @param copyrightDTO
     * @return
     */
    @PostMapping("contract/copyrights/status/amount")
    public Result setStatusAndAmount(@RequestBody CopyrightDTO copyrightDTO){
        boolean success = contractCopyrightService.setStatusAndAmount(copyrightDTO.getUuid(),CustomerConstant.TRANSFER_STATUS,copyrightDTO.getAmount());
        if (!success) {
            return Result.failed("设置版权转让失败");
        }
        return Result.succeed("设置版权转让成功");
    }

}
